<template>
	<view style="overflow-x: hidden;">
		<view class="verify u-f-ajc u-f-c">
			<image src="https://statics.xmcsrv.net/weixin/userManage/verifyPwd.png" class="img" mode=""></image>
			<view class="verifyDesc">请输入6-12位数字密码</view>
		</view>
		<view class="code">
			<!-- <xt-verify-code size="6" ref="verifyCode" :isFocus="false" inputType="number" cursorColor="#000"
				class="verify-code" v-model="password" @confirm="vcodeInput">
			</xt-verify-code> -->
			<input class="input-code" :focus="true" type="number" maxlength="12" v-model="password" />
		</view>
		<button class="btn radius-btn" :type="password.length >= 6?'operation':''" @tap="vcodeInput()">确定</button>
		<j-modal :value="confirmModal" :maskCloseAble="false" title="您当前设置的密码为" okText="确定" cancelText="取消" @ok="onOk"
			@cancel="onCancel">
			<template slot="content">
				<view class="password">
					{{password}}
				</view>
				<view class="confirm-text">
					是否确认？
				</view>
			</template>
		</j-modal>
	</view>
</template>

<script>
	const {
		turnTenToSixteen
	} = require('@/utils/util.js');
	const {
		handledoorLockTransparent
	} = require('@/jvss/pagesDevice/utils/index.js');
	export default {
		data() {
			return {
				password: '',
				authType: 1, //0是管理员 1是普通用户
				userInfo: {},
				device: {},
				confirmModal: false,
			}
		},
		onLoad(opt) {
			this.authType = opt.authType
			console.log('autuType:===>', opt.authType)
			let enData = decodeURIComponent(opt.device)
			this.device = JSON.parse(enData)
			if (opt.userInfo) {
				this.userInfo = JSON.parse(opt.userInfo)
			}
			console.log('userInfo', this.userInfo)
			console.log('验证密码===', opt);
		},
		onShow() {
			if (this.authType == 0) {
				uni.setNavigationBarTitle({
					title: '添加管理员'
				})
			} else {
				uni.setNavigationBarTitle({
					title: '添加普通用户'
				})
			}
		},
		methods: {
			//确认输入密码
			vcodeInput() {
				if (this.password.length < 6) {
					return
				}
				this.verifyManagerPwd()
			},
			verifyManagerPwd() {
				// 	弹出确认框，确认后添加并返回上一页
				this.confirmModal = true
			},
			onOk() {
				uni.showLoading()
				let data = {
					Name: 'OPDoorLockProCmd',
					OPDoorLockProCmd: {
						Cmd: "DoorUser",
						DoorUser: {
							Action: 'AddUser',
							Auth: parseInt(this.authType),
							Type: this.userInfo.type,
							Pswd: this.userInfo.id == 'password' ? this.password : ''
						}
					},
				};
				let token = this.device.token;
				getApp().globalData.api.doorLockTransparent(data, token, (result) => {
					console.log(result, '添加用户返回结果')
					uni.hideLoading()
					handledoorLockTransparent(result, (res, Err) => {
						console.log(res, Err, 'handledoorLockTransparent')
						if (Err == '0x00') {
							uni.showToast({
								title: '添加成功',
								icon: 'success',
								duration: 2000
							})
							setTimeout(() => {
								uni.navigateBack({})
							}, 1000)
						} else {
							if (Err === '0x0f' || Err === '0x10') {
								uni.showToast({
									title: '请先添加或修改管理员密码',
									icon: 'none',
									duration: 2000
								})
								setTimeout(() => {
									uni.navigateBack({
										delta: 2
									})
								}, 1000)
							} else {
								uni.showToast({
									title: getApp().globalData.errCodeMsg.wakeUpLockBoardCodeMsg[
										Err] || result.data.msg,
									icon: 'none',
									duration: 2000
								});
								setTimeout(() => {
									uni.navigateBack({})
								}, 1000)
							}
						}
					}, true)
				}, false)
			},
			onCancel() {
				this.confirmModal = false
			},
			// 发送End结束流程
			sendEnd(Err, result) {
				uni.showToast({
					title: getApp().globalData.errCodeMsg.wakeUpLockBoardCodeMsg[Err] || result.data.msg,
					icon: 'none',
					duration: 2000
				});
				let data = {
					Name: 'OPDoorLockProCmd',
					OPDoorLockProCmd: {
						Cmd: "SyncDoorStatus",
						DoorStatus: {
							Status: 'End'
						},
						SessionID: 0x00000001
					}
				};
				let token = this.device.token;
				getApp().globalData.api.doorLockTransparent(data, token, (result) => {
					console.log('发送End', result);
					if (result.data.code == 2000) {
						setTimeout(() => {
							uni.navigateBack({})
						}, 1000)
					} else {
						uni.showToast({
							title: getApp().globalData.errCodeMsg.CodeMsg[result.data.code] || result.data
								.msg,
							icon: 'none',
							duration: 2000
						});
						uni.navigateBack({})
					}
				}, false);
			}
		}
	}
</script>

<style lang="scss">
	.input-code {
		text-align: center;
		font-size: 20px;
		color: #FF0025;
		font-weight: bold;
		// border-bottom: 1px solid #B9B9B9;
		// padding-bottom: 10px;
		border: 1px solid #B9B9B9;
		padding: 10px;
		border-radius: 10px;
		background-color: #fff;
	}

	.verify-code {
		.xt__verify-code {
			margin: auto;
		}

		.xt__input-ground {
			margin: auto;

			.xt__code-text {
				font-size: 40rpx !important;
			}
		}

		/deep/.xt__code-text {
			font-size: 40rpx !important;
		}

		/deep/.xt__box {
			width: 70rpx !important;
			height: 70rpx !important;
			background: #fff;
			border-radius: 10rpx;
			border: #eee;
			border-width: 1px !important;
			border-style: solid !important;
		}
	}

	.verify {
		margin-top: 150rpx;

		.img {
			width: 376rpx;
			height: 267.5rpx;
		}

		.verifyDesc {
			font-size: 26rpx;
		}
	}

	.code {
		width: 86%;
		margin: auto;
		margin-top: 80rpx;
	}

	.find-food-submit {
		border: 1px solid #B9B9B9;
		border-radius: 16rpx;
		background-color: #fff !important;
		color: #FF0025 !important;
		width: 96%;
		height: 80rpx;
		line-height: 80rpx;
		margin-top: 30rpx;
	}

	.btn {
		background: #B9B9B9;
		color: white;
		// border-radius: 16rpx;
		width: 96%;
		height: 80rpx;
		margin-top: 30rpx;
	}

	.password {
		margin-left: 10px;
		margin-top: -5px;
		text-align: center;
		color: black;
		font-size: 32px;
		font-weight: bold;
		color: #FF0025;
		// letter-spacing: .4em;
	}

	.confirm-text {
		margin-top: 10px;
		color: black;
		font-size: 13px;
		font-weight: bold;
	}

	button {
		// border-radius: 40rpx;
		width: 86% !important;
		height: 80rpx;
		margin-top: 30rpx;
	}
</style>